package com.itheima.service.sms.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.core.model.PageResult;
import com.itheima.common.exception.CustomException;
import com.itheima.common.utils.StringUtils;
import com.itheima.mapper.sms.SmsFlashPromotionMapper;
import com.itheima.pojo.sms.SmsFlashPromotion;
import com.itheima.service.sms.SmsFlashPromotionService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class SmsFlashPromotionServiceImpl extends ServiceImpl<SmsFlashPromotionMapper, SmsFlashPromotion> implements SmsFlashPromotionService {
    /**
     * 添加活动
     *
     * @param smsFlashPromotion
     * @return
     */
    @Override
    public int create(SmsFlashPromotion smsFlashPromotion) {
        if (smsFlashPromotion == null) {
            throw new CustomException("数据错误");
        }
        //添加活动
        boolean result = this.save(smsFlashPromotion);
        return result ? 1 : 0;
    }

    /**
     * 删除活动
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {
        boolean result = this.removeById(id);
        return result ? 1 : 0;
    }

    /**
     * 根据活动名称分页查询
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResult selectPage(String keyword, Integer pageNum, Integer pageSize) {

        //构造分页构造器
        Page<SmsFlashPromotion> pageInfo = new Page<>(pageNum, pageSize);

        //构造条件构造器
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(keyword), SmsFlashPromotion::getTitle, keyword);
        this.page(pageInfo, lqw);

        return PageResult.init((int) pageInfo.getTotal(), pageSize, pageNum, pageInfo.getRecords());
    }

    /**
     * 修改上下线状态
     *
     * @param id
     * @param smsFlashPromotion
     * @return
     */
    @Override
    public int updateStatus(Long id, SmsFlashPromotion smsFlashPromotion) {
        //构造条件构造器
        LambdaUpdateWrapper<SmsFlashPromotion> luw = new LambdaUpdateWrapper<>();
        luw.eq(id != null, SmsFlashPromotion::getId, id);
        luw.set(smsFlashPromotion.getStatus() != null, SmsFlashPromotion::getStatus, smsFlashPromotion.getStatus());
        boolean result = this.update(luw);
        return result ? 1 : 0;
    }

    /**
     * 编辑活动
     *
     * @param id
     * @param smsFlashPromotion
     * @return
     */
    @Override
    public int updateFlashPromotion(Long id, SmsFlashPromotion smsFlashPromotion) {
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null, SmsFlashPromotion::getId, id);
        boolean result = this.update(smsFlashPromotion, lqw);
        return result ? 1 : 0;
    }

    /**
     * 获取活动详情
     *
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotion selectById(Long id) {
        return this.getById(id);
    }
}
